<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\DownloadTemplate;
use App\Admin\Forms\GzdImportForm;
use App\Models\GzdWorkUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Support\Facades\DB;

class GzdWorkUserController extends AdminController
{
    protected $title = '人员库';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GzdWorkUser(), function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
//            $grid->export()->disableExportAll();
            $grid->perPages([10, 20, 30, 40, 50,100,200,300,400,500]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('engineering', '所属工程')->select(function ($status){
                    $Adminid  = Admin::user()->id;
                    if($Adminid != 1 && Admin::user()->whole != 0){
                        $engineering_name = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->where('deleted_at',null)->pluck('engineering_name');
                    }else{
                        $engineering_name = DB::table('engineering')->where('deleted_at',null)->pluck('engineering_name');
                    }
                    $arr = [];
                    foreach ($engineering_name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);
                $filter->like('id_card', '身份证号')->width(5);
                $filter->like('name', '姓名')->width(5);
                $filter->equal('mobile','手机号')->width(5);

                $filter->group('team', function ($group) {
                    $group->like('模糊查找');
                    $group->equal('精确查找');
                },'所属队伍')->width(5);


                $filter->like('company', '所属公司')->select(function ($team){
                    $name = DB::table('company')->where('deleted_at',null)->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

                $filter->equal('type_work','工种')->select(function ($team){
                    $name = DB::table('type_work')->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

                $filter->equal('position','职务')->select(['一般工人', '班组长', '其他'])->width(5);

                $filter->between('enter_time', '入职时间')->date()->width(5);

            });



            $grid->number('序号');

            $grid->column('name','姓名');

            $grid->column('company','所属公司');
            $grid->column('team','所属队伍');
            $grid->column('engineering','所属工程')->limit(10);

            $grid->column('type_work','工种');
            $grid->column('id_card','身份证号');
//            $grid->column('position','职务')->select(['一般工人', '班组长', '其他']);
            $grid->column('age','年龄');
            $grid->column('uploader','数据来源')->display(function ($uploader) {
                return DB::table('admin_users')->where('id',$uploader)->value('username');
            });


            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(Modal::make()
                    // 大号弹窗
                    ->lg()
                    // 弹窗标题
                    ->title('上传文件')
                    // 按钮
                    ->button('<button class="btn btn-primary"><i class="feather icon-upload"></i> 导入人员数据</button>')
                    // 弹窗内容
                    ->body(GzdImportForm::make()));
                // 下载导入模板
                $tools->append(DownloadTemplate::make()->setKey('test_question'));
            });


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GzdWorkUser(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('photo_portrait');
            $show->field('photo_emblem');
            $show->field('id_card');
            $show->field('type_work');
            $show->field('bank_deposit');
            $show->field('bank_number');
            $show->field('engineering');
            $show->field('registered_residence');
            $show->field('mobile');
            $show->field('educational');
            $show->field('employment_method');
            $show->field('marital');
            $show->field('emergency_contact');
            $show->field('emergency_contact_mobile');
            $show->field('status');
            $show->field('misdeed');
            $show->field('notes');
            $show->field('company');
            $show->field('team');
            $show->field('enter_time');
            $show->field('exit_time');
            $show->field('age');
            $show->field('enter_date');
            $show->field('exit_date');
            $show->field('uploader');
            $show->field('position');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GzdWorkUser(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('photo_portrait');
            $form->text('photo_emblem');
            $form->text('id_card');
            $form->text('type_work');
            $form->text('bank_deposit');
            $form->text('bank_number');
            $form->text('engineering');
            $form->text('registered_residence');
            $form->text('mobile');
            $form->text('educational');
            $form->text('employment_method');
            $form->text('marital');
            $form->text('emergency_contact');
            $form->text('emergency_contact_mobile');
            $form->text('status');
            $form->text('misdeed');
            $form->text('notes');
            $form->text('company');
            $form->text('team');
            $form->text('enter_time');
            $form->text('exit_time');
            $form->text('age');
            $form->text('enter_date');
            $form->text('exit_date');
            $form->text('uploader');
            $form->text('position');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
